package app.fugema.shop.main.order.adapter;

import android.app.Activity;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;
import app.fugema.shop.main.wallet.model.BuyBean;
import app.fugema.shop.main.wallet.view.WalletPayDialogFragment;

import com.base.common.app.AppManager;
import com.base.common.app.LoginUtils;
import com.base.common.model.bean.ADInfo;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.base.BaseActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.OrderListItemButtonBinding;
import app.fugema.shop.main.order.view.DeliveryCheckDetailActivity;
import app.fugema.shop.main.order.vm.OrderListVM;

import java.util.ArrayList;
import java.util.List;

public class OrderListItemButtonAdapter extends SingleRecyclerAdapter<ADInfo, OrderListItemButtonBinding> {

    private OrderListVM orderListVM;

    public OrderListItemButtonAdapter(LifecycleOwner lifecycleOwner, OrderListVM orderListVM) {
        super(lifecycleOwner);
        this.orderListVM = orderListVM;
    }

    public OrderListVM getOrderListVM() {
        return orderListVM;
    }

    //status  1:待付款 2:待发货 3:待收货 4:待评价 5:成功 6:失败
    public static List<ADInfo> getStatesButton(int status) {
        switch (status) {
            case 1:
                return getStatusButton(5, 6);
            case 2:
                return getStatusButton(8);
            case 3:
                return getStatusButton(1, 10);
            case 4:
            case 5:
                return getStatusButton(3);
            case 6:
                return getStatusButton(4);
            default:
                return getStatusButton(0);
        }
    }

    private static List<ADInfo> getStatusButton(int... states) {
        List<ADInfo> list = new ArrayList<>();
        for (int state : states) {
            switch (state) {
                case 1:
                    list.add(createAdInfo(1, "查看物流"));
                    break;
                case 2:
//                    list.add(createAdInfo(2, "评价"));
                    break;
                case 3:
                    list.add(createAdInfo(3, "再来一单"));
                    break;
                case 4:
                    list.add(createAdInfo(4, "删除订单"));
                    break;
                case 5:
                    list.add(createAdInfo(5, "取消订单"));
                    break;
                case 6:
                    list.add(createAdInfo(6, "付款"));
                    break;
                case 7:
//                    list.add(createAdInfo(7, "退款"));
                    break;
                case 8:
                    list.add(createAdInfo(8, "提醒发货"));
                    break;
                case 9:
                    list.add(createAdInfo(9, "取消退款"));
                    break;
                case 10:
                    list.add(createAdInfo(10, "确认收货"));
                    break;
            }
        }
        return list;
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, ADInfo bean) {
        super.onItemClick(view, adapter, onclickType, position, bean);
        String userKey = LoginUtils.getToken();
        switch (bean.getId())
        {
            case 1:
                DeliveryCheckDetailActivity.start(view.getContext(), bean.getImageId());
                break;
            case 2:
                break;
            case 3:
                buyAgain(bean.getImageId());//再来一单
                break;
            case 4:
                UIUtils.showToastSafes("关闭订单");
                closeOrder(Long.parseLong("2021053140096116"), userKey);
                break;
            case 5:
                orderCancel(bean.getImageId());
                break;
            case 6://付款
                Activity activity = AppManager.getInstance().getTopActivity();
                if (activity instanceof BaseActivity) {
                    BaseActivity baseActivity = (BaseActivity) activity;
                    BuyBean buyBean = new BuyBean();
//                        buyBean.setMoney(data.data.getData().getActualTotal());
                    buyBean.setMoney(bean.getPrice());
                    buyBean.setOrderNumbers(bean.getImageId());
                    baseActivity.addFragment(WalletPayDialogFragment.newInstance(buyBean));
                }

                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;
            case 10:
                orderReceipt(bean.getImageId());
                break;
        }

    }

    private static ADInfo createAdInfo(int state, String text) {
        ADInfo adInfo = new ADInfo();
        adInfo.setId(state);
        adInfo.setImageName(text);
        switch (state) {
            case 2:
            case 3:
            case 6:
            case 9:
                adInfo.setSelected(true);
                break;
        }
        return adInfo;
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.order_list_item_button;
    }


    //再来一单
    private void buyAgain(String orderNumber)
    {
        UIUtils.showToastSafes("再来一单");
    }


    //确认收货
    private void orderReceipt(String orderNumber)
    {
        UIUtils.showToastSafes("确认收货");
        /*
        getOrderListVM().orderReceipt(orderNumber).observe(getLifecycleOwner(), new BaseViewObserver<LiveDataWrapper<BaseResponse>>(AppManager.getInstance().getTopActivity()) {
            @Override
            protected void onSuccess(LiveDataWrapper<BaseResponse> data) {
                UIUtils.showToastSafes("确认收货成功");
                LiveEventBus.get(AppConstants.EventKey.ORDER_RECEIPT, String.class).post(orderNumber);
            }
        });
        */
    }


    //删除订单
    private void closeOrder(long orderId,String userKey )
    {
        getOrderListVM().closeOrder(orderId,userKey).observe(getLifecycleOwner(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(AppManager.getInstance().getTopActivity())
        {
            @Override
            protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
            {
                UIUtils.showToastSafes("订单已关闭");
                LiveEventBus.get(AppConstants.EventKey.ORDER_CANCEL, String.class).post(""+orderId);
            }
        });
    }


    //取消订单
    private void orderCancel(String orderNumber)
    {
        UIUtils.showToastSafes("取消订单");
        /*
        if (AppManager.getInstance().getTopActivity() instanceof BaseActivity)
        {
            BaseActivity activity = (BaseActivity) AppManager.getInstance().getTopActivity();
            activity.addFragment(rderCancelDialogFragment);
        }
        */
    }


}
